﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Client
{
    public class TxtReader
    {
        // 관리되고 있는 프로젝트의 정보를 담고 있는 텍스프파일 이름
        const string txtProjectFileName = "\\test.txt";
        private static string[] line = new string[(int)LINE.DATE+1];

        const string txtSystemFileName = "system.txt";

        public static string GetString(LINE lineType)
        {
            return line[(int)lineType];
        }

        public enum LINE
        {
            PROJECTID = 0,
            VERSION = 1,
            DATE = 2
        }

        // Create Project 이벤트 실행시
        // Project 폴더 내부에 파일을 생성
        public static void CreateDataFile(string path)
        {
            string fileName = path + txtProjectFileName;
            FileStream fs = File.Create(fileName);
            fs.Close();
        }

        public static void ReadDataFile(string path)
        {
            System.IO.StreamReader file = new System.IO.StreamReader(@path + txtProjectFileName);
            int index = 0;
            while ((line[index++] = file.ReadLine()) != null)
            {
                if(index == (int)LINE.DATE + 1)
                    break;
            }
            file.Close();
        }

        public static void WriteDataFile(string path, string[] data)
        {
            System.IO.StreamWriter file = new System.IO.StreamWriter(@path + txtProjectFileName);
            foreach (string line in data)
            {
                file.WriteLine(line);
            }
            file.Close();
        }



        public static void CreateSystemFile()
        {
            try
            {
                System.IO.FileStream fs = new System.IO.FileStream(@txtSystemFileName, FileMode.CreateNew);
                fs.Close();
            }
            catch (System.IO.IOException ex)
            {
                
            }
        }

        // System File에서 projectID를 참조하여 Project 설치 경로를 반환
        public static string FindPathForProject(string projectID)
        {
            System.IO.StreamReader file = new System.IO.StreamReader(@txtSystemFileName);
            string line;
            while((line = file.ReadLine()) != null)
            {
                if (line.Contains(projectID) == true)
                {
                    string ret = line.Remove(0, projectID.Length);
                    file.Close();
                    return ret;
                }
            }
            file.Close();
            return null;
        }

        public static void WriteSystemFile(string projectID, string adr)
        {
            System.IO.StreamWriter file = new System.IO.StreamWriter(@txtSystemFileName);
            file.WriteLine(projectID + adr);
            file.Close();
            
        }
    }
}
